//
//  MyDataViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/28.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "MyDataViewController.h"
#import "MyDataTableViewCell.h"
#import "NewNickViewController.h"
#import "BirthdayViewController.h"
#import "MyVoiceViewController.h"
#import "NewImageViewController.h"
#import "PlayerManager.h"
#import "MHDataHelper.h"
#import "MyCenterViewController.h"
#import "RecordViewController.h"

@interface MyDataViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIImageView *backgroundView;
@end

@implementation MyDataViewController

//类方法，单例对象(该单例比较特殊，记得回顾)
+(instancetype)sharedMyDataViewController{
    static MyDataViewController *myDataVC = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        myDataVC = [[MyDataViewController alloc]init];
    });
    return myDataVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];    
    //   背景图片
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.image = [UIImage imageNamed:@"100.jpg"];
    [self.view addSubview:self.backgroundView];
    [self.view sendSubviewToBack:self.backgroundView];
    self.title = @"个人资料";
    self.edgesForExtendedLayout = UIRectEdgeNone;
//    添加 返回中心 事件
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"个人中心" style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
//    设置头像button 添加点击事件
    self.ImageBtn.layer.masksToBounds = YES;
    self.ImageBtn.layer.cornerRadius = self.ImageBtn.frame.size.width/2.0;
    [self.ImageBtn addTarget:self action:@selector(newImage) forControlEvents:(UIControlEventTouchUpInside)];
//    注册cell
    UINib *nib = [UINib nibWithNibName:@"MyDataTableViewCell" bundle:nil];
    [self.myDataTV registerNib:nib forCellReuseIdentifier:@"cell"];
//    设置tableview代理
    self.myDataTV.dataSource = self;
    self.myDataTV.delegate = self;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
//设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     MyDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.label1.font = [UIFont systemFontOfSize:18];
    cell.label2.font = [UIFont systemFontOfSize:18];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    
    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    
    if (indexPath.row == 0) {
        cell.label1.text = @"昵称";
        if (status == RealStatusNotReachable){
            cell.label2.text = [user1 objectForKey:@"nickname"];
        }else{
        cell.label2.text = [AVUser currentUser][@"nickname"];
        }
    }else if (indexPath.row == 1){
        cell.label1.text = @"声音签名";
        cell.label2.text = @"快来录制吧";
    }else if (indexPath.row == 2){
        cell.label1.text = @"性别";
        if (status == RealStatusNotReachable){
            cell.label2.text = [user1 objectForKey:@"gender"];
        }else{
        cell.label2.text = [AVUser currentUser][@"gender"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else if (indexPath.row == 3){
        cell.label1.text = @"出生日期";
        if (status == RealStatusNotReachable){
            cell.label2.text = [user1 objectForKey:@"birthday"];
        }else{
        cell.label2.text = [AVUser currentUser][@"birthday"];
        }
    }
    return cell;
}
//cell点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        NewNickViewController *newNickVC = [[NewNickViewController alloc]init];
        [self.navigationController pushViewController:newNickVC animated:YES];
    }else if (indexPath.row == 1){
        MyVoiceViewController *myVoice = [MyVoiceViewController new];
        [self.navigationController pushViewController:myVoice animated:YES];
    }else if (indexPath.row == 3){
        BirthdayViewController *birthdayVC = [BirthdayViewController sharedMyBirthdayViewController];
        [self.navigationController pushViewController:birthdayVC animated:YES];
    }
}
//选新头像 跳转
-(void)newImage{
    NewImageViewController *newImageVC = [NewImageViewController new];
    [self.navigationController pushViewController:newImageVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}
//返回个人中心 跳转事件
-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
//出现 隐藏tabbar
-(void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:YES];

    RealReachability *reachability = [RealReachability sharedInstance];
    ReachabilityStatus status = [reachability currentReachabilityStatus];
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    if (status == RealStatusNotReachable){
        [self.ImageBtn setBackgroundImage:[UIImage imageNamed:[user1 objectForKey:@"userImageName"]] forState:(UIControlStateNormal)];
    }else{
        [self.ImageBtn setBackgroundImage:[UIImage imageNamed:[AVUser currentUser][@"userImageName"]] forState:(UIControlStateNormal)];
    }
    
    [self.myDataTV reloadData];
}



@end
